package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.BoardMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.services.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        // 非空判断
        if (num <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 查询num条数据
        return boardMapper.selectByNum(num);
    }

    @Override
    public void addOneArticleCountById(Long id) {
        // 非空判断
        if(id == null || id <= 0) {
            log.warn (ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            // 当前板块查询不存在
            log.warn(ResultCode.ERROR_IS_NULL .toString() + " boardId: " + id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL ));
        }
        // 增加帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        // 调用DAO
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + ",受影响的行数不等于1 ");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        if(id == null || id <= 0) {
            log.warn (ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        // 调用DAO查询数据
        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if (id == null || id <= 0) {
            log.warn (ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            // 当前板块查询不存在
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + " boardId: " + id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        // 减少帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);

        if (updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }

        // 调用DAO
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row!= 1) {
            log.warn(ResultCode.FAILED.toString() + ",受影响的行数不等于1 ");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }
}
